<?php
/**
 * 分销提现申请模型
 */

namespace app\model;

use think\Model;

class DistributionWithdrawalModel extends Model
{
    // 设置表名
    protected $name = 'sh_distribution_withdrawal';
    
    // 设置主键
    protected $pk = 'id';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 获取提现申请列表
     */
    public static function getWithdrawalRecords($filter = [], $page = 1, $limit = 15)
    {
        $query = self::alias('dw')
            ->join('customer c', 'dw.customer_id = c.id')
            ->field('dw.*, c.name as customer_name, c.telephone, c.avatar');
            
        // 根据客户姓名筛选
        if (!empty($filter['customer_name'])) {
            $query->where('c.name', 'like', '%' . $filter['customer_name'] . '%');
        }
        
        // 根据状态筛选
        if (!empty($filter['status'])) {
            $query->where('dw.status', $filter['status']);
        }
        
        $list = $query->page($page, $limit)->order('dw.create_time', 'desc')->select();
        $total = $query->count();
        
        return [
            'list' => $list,
            'total' => $total
        ];
    }
    
    /**
     * 获取提现申请详情
     */
    public static function getWithdrawalInfo($id)
    {
        return self::alias('dw')
            ->join('customer c', 'dw.customer_id = c.id')
            ->field('dw.*, c.name as customer_name, c.telephone, c.avatar')
            ->where('dw.id', $id)
            ->find();
    }
    
    /**
     * 添加提现申请
     */
    public static function addWithdrawalRecord($data)
    {
        return self::create($data);
    }
    
    /**
     * 更新提现申请
     */
    public static function updateWithdrawalRecord($id, $data)
    {
        return self::where(['id' => $id])->update($data);
    }
    
    /**
     * 删除提现申请
     */
    public static function deleteWithdrawalRecord($id)
    {
        return self::where(['id' => $id])->delete();
    }
}